package cn.com.adcc.utils;

import net.jpountz.lz4.LZ4Compressor;
import net.jpountz.lz4.LZ4Factory;
import net.jpountz.lz4.LZ4FastDecompressor;
import net.jpountz.lz4.LZ4SafeDecompressor;
import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.util.Zip4jConstants;
import org.apache.log4j.Logger;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;

import java.io.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.concurrent.*;

/**
 * Created by anzilu on 2015/10/10.
 */
public class ZipExtract {

    private static Logger logger = Logger.getLogger(Configuration.class);

    public static void compressFiles(String source,String destination){

        new Thread(new ZipHandler(source,destination)).start();
    }

    public static void extractFiles(String source,String destination){

        new Thread(new ExtracHandler(source,destination)).start();
    }

    public static boolean compressStream(InputStream is,String destination,String fileNameInZip){

        try {
            ZipFile zipFile = new ZipFile(destination);
            ZipParameters parameters = new ZipParameters();
            parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
            parameters.setFileNameInZip(fileNameInZip);
            parameters.setSourceExternalStream(true);
            zipFile.addStream(is, parameters);
            return true;
        } catch (Exception e) {
            logger.error(e.getMessage());
            return false;
        }

    }


    public static void compressLZ4()throws UnsupportedEncodingException {

        LZ4Factory factory = LZ4Factory.fastestInstance();

        byte[] data = "12345345234572".getBytes("UTF-8");
        final int decompressedLength = data.length;

        // compress data
        LZ4Compressor compressor = factory.fastCompressor();
        int maxCompressedLength = compressor.maxCompressedLength(decompressedLength);
        byte[] compressed = new byte[maxCompressedLength];
        int compressedLength = compressor.compress(data, 0, decompressedLength, compressed, 0, maxCompressedLength);

        // decompress data
        // - method 1: when the decompressed length is known
        LZ4FastDecompressor decompressor = factory.fastDecompressor();
        byte[] restored = new byte[decompressedLength];
        int compressedLength2 = decompressor.decompress(compressed, 0, restored, 0, decompressedLength);
        // compressedLength == compressedLength2

        // - method 2: when the compressed length is known (a little slower)
        // the destination buffer needs to be over-sized
        LZ4SafeDecompressor decompressor2 = factory.safeDecompressor();
        int decompressedLength2 = decompressor2.decompress(compressed, 0, compressedLength, restored, 0);
        // decompressedLength == decompressedLength2

    }


    public static void main(String[] args) throws FileNotFoundException {

//        try {
//            compressLZ4();
//        }
//        catch (UnsupportedEncodingException e) {
//            e.printStackTrace();
//        }

        //String zipSource="F:\\GFS\\20150915\\00";
        String zipSource="D:\\MSVS2013";
        String zipDestinaton="D:\\MSVS2013.zip";
        DateTime startcomp=new DateTime(DateTimeZone.UTC);
        System.out.println(startcomp.getMillis());
        compressFiles(zipSource,zipDestinaton);

//        System.out.println("Continued 1...");
//        String extractDestination="D:\\zipdestination";
//        extractFiles(zipDestinaton, extractDestination);
//        String extractDestination="D:\\zipdestination";
//        DateTime startextract=new DateTime(DateTimeZone.UTC);
//        extractFiles(zipDestinaton,extractDestination);

//            DateTime endextract=new DateTime(DateTimeZone.UTC);
//            System.out.println("Extraction costs: "+(endextract.getMillis()-startextract.getMillis())+" milliseconds");
//            System.out.println("解压缩成功："+extractDestination);

//        System.out.println("Continued 2...");
//        InputStream is= new FileInputStream("D:\\zipsource\\team.docx");
//        String destination="D:\\ziptest\\addstreamtozip.zip";
//        compressStream(is,destination,"streamraw");
//        if (is != null) {
//            try {
//                is.close();
//            }
//            catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
//        System.out.println("Continued 3...");
//        String extractDestination2="D:\\ziptest\\stream";
//        extractFiles(destination,extractDestination2);
    }


}
